import { t } from 'i18next'
import { uniqBy } from 'lodash-es'
import { InitOption } from '../interface'
import { CONSTANT } from '../utils/utils'
import { v4 as uuidv4 } from 'uuid'
import { DepartmentUser, isIpInPending, isUser, PendingItem, PendingMtModel, Template } from './pending'

export interface SelectMtEntity {
	account?: string
	accountType?: number
	name?: string
	protocol?: 0 | 1 | 2 | 255
	bitrate?: number
	bitrateType?: number
	portrait?: string
	moid?: string
	e164?: string
	mobilePhone?: string
	workPhone?: string
	// isSimpleCascade?: boolean
	meetingType?: null | 0 | 1 | 2
	oftenMt?: 0 | 1
	interactiveMt?: 0 | 1
}
export interface SelectedMt {
	account: string
	accountType: number
	name: string
	protocol: 0 | 1 | 2 | 255
	bitrate: number
	bitrateType: number
	portrait: string
	moid: string
	e164: string
	mobilePhone: string
	workPhone: string
}
export class SelectedMtModel {
	id: string
	item: SelectedMt
	attr: {
		isOftenMt: boolean //常用终端
		isInteractiveMt: boolean //互动终端
		isMeeting: boolean //是否是会议
		meetingType: null | 0 | 1 | 2 //会议是什么会议
		// isSimpleCascade: boolean //是否简单级联会议
	}
	status: {
		isSelected: boolean
		clickTimestamp: number | null
		isOpenMoreOpt: boolean
	}
	constructor(mt: SelectMtEntity) {
		this.id = uuidv4()
		this.item = {
			account: mt.account || '',
			accountType: mt.accountType || 0,
			name: mt.name || '',
			bitrate: mt.bitrate || 2048,
			bitrateType: transBitrateValueToType(mt.bitrate || 2048),
			protocol: mt.protocol != null ? mt.protocol : CONSTANT.callType.AUTO,
			portrait: mt.portrait || '',
			moid: mt.moid || '',
			e164: mt.e164 || '',
			mobilePhone: mt.mobilePhone || '',
			workPhone: mt.workPhone || '',
		}
		this.attr = {
			isOftenMt: !!mt.oftenMt || false, //常用终端
			isInteractiveMt: !!mt.interactiveMt || false, //互动终端
			isMeeting: mt.accountType === CONSTANT.accountType.template, //是否是会议
			meetingType: mt.meetingType != null ? mt.meetingType : null, //会议是什么会议
			// isSimpleCascade: mt.isSimpleCascade || false, //是否简单级联会议
		}
		this.status = {
			isSelected: false,
			clickTimestamp: null,
			isOpenMoreOpt: false,
		}
	}
}
function transBitrateValueToType(val: number) {
	switch (val) {
		case 512:
			return 1
		case 768:
			return 2
		case 1024:
			return 3
		case 2048:
			return 4
		case 3072:
			return 5
		case 4096:
			return 6
		case 8192:
			return 7
		default:
			return 0
	}
}
export type SelectedListModel = SelectedMtModel[]
export function isIpInSelected(data: SelectedMtModel) {
	return data.item.accountType === CONSTANT.accountType.IP || data.item.accountType === CONSTANT.accountType.e164Ip
}
/**
 * @description 获得码率下拉列表
 * @param maxRate
 */
export const getRateList = (maxRate: number) => {
	var _data = []
	if (maxRate % 1024 !== 0 && maxRate !== 512 && maxRate !== 768) {
		_data.push({ id: maxRate, name: maxRate + 'Kbps' })
	}
	if (maxRate >= 1024 * 8) {
		_data.push({ id: 7, name: '8M' })
	}
	if (maxRate >= 1024 * 4) {
		_data.push({ id: 6, name: '4M' })
	}
	if (maxRate >= 1024 * 3) {
		_data.push({ id: 5, name: '3M' })
	}
	if (maxRate >= 1024 * 2) {
		_data.push({ id: 4, name: '2M' })
	}
	if (maxRate >= 1024) {
		_data.push({ id: 3, name: '1M' })
	}
	if (maxRate >= 768) {
		_data.push({ id: 2, name: '768Kbps' })
	}
	if (maxRate >= 512) {
		_data.push({ id: 1, name: '512Kbps' })
	}
	_data.push({ id: 0, name: t('自定义') })
	return _data
}
/**
 * @description 码率下拉-> 码率值
 * @param bitrate
 */
export const getBitrate = (bitrate: number) => {
	switch (bitrate) {
		case 0:
			return 0
		case 1:
			return 512
		case 2:
			return 768
		case 3:
			return 1024
		case 4:
			return 2048
		case 5:
			return 3072
		case 6:
			return 4096
		case 7:
			return 8192
		default:
			return bitrate
	}
}
export function selectedMtEntityTranslator(
	data: PendingItem<DepartmentUser | Template>,
	callType: 'video' | 'audio' | 'phone' | 'auto',
	{ isMcc, isSuperMeeting, maxRate }: Pick<InitOption, 'isMcc' | 'isSuperMeeting' | 'maxRate'>
) {
	let user: SelectMtEntity = {
		name: data.item.name,
		bitrate: maxRate, //码率 默认设置为会议码率
		bitrateType: 0,
		e164: data.item.e164,
	}
	if (isUser(data.item)) {
		//左侧添加到右侧，只要不是模板，必是组织架构内的，必有 moid
		user.portrait = data.item.portrait
		user.moid = data.item.moid
		user.mobilePhone = data.item.mobilePhone
		user.workPhone = data.item.workPhone
		if (
			data.attr.fromSearchVal &&
			CONSTANT.reg.mobilePhoneReg.test(data.attr.fromSearchVal) &&
			data.item.mobilePhone &&
			data.item.mobilePhone.includes(data.attr.fromSearchVal)
		) {
			//1.搜索是电话，并且与搜出来的user里电话匹配成功，取电话呼叫方式
			//warning 实际上后台不会检索 workphone。
			user.account = data.item.mobilePhone
			user.accountType = CONSTANT.accountType.phone
			user.protocol = CONSTANT.callType.AUTO
		} else if (data.item.ip) {
			//2. ip呼叫方式
			let ip = data.item.ip
			//ip 字段里，可能存在纯数字，纯数字当作 e164号 呼叫，其他ip就是当ip呼
			//warning sipUrl邮箱的设备录入 已经被移除了！
			if (isIpInPending(ip)) {
				user.account = ip
				if (CONSTANT.reg.regIP.test(ip)) {
					user.accountType = CONSTANT.accountType.IP
				} else if (CONSTANT.reg.e164Ip.test(ip)) {
					user.accountType = CONSTANT.accountType.e164Ip
				}
				//protocol 除了ip需要走后台的值，其他都默认赋值 Auto
				user.protocol = data.item.protocol != null ? data.item.protocol : CONSTANT.callType.AUTO
			} else {
				user.account = ip
				user.accountType = CONSTANT.accountType.sat
				user.protocol = CONSTANT.callType.AUTO
			}
		} else {
			//3.正常moid呼叫
			user.account = data.item.moid
			user.accountType = CONSTANT.accountType.softTerminal
			user.protocol = CONSTANT.callType.AUTO
		}
		//4.走手动呼叫类型
		if (callType === 'phone') {
			user.account = data.item.mobilePhone || data.item.workPhone
			user.accountType = CONSTANT.accountType.phone
			user.protocol = CONSTANT.callType.AUTO
		} else if (callType === 'audio') {
			user.bitrate = CONSTANT.bitrate.addByAudio
		}
	} else {
		user.account = data.item.e164
		user.accountType = CONSTANT.accountType.template
		user.protocol = CONSTANT.callType.AUTO
		user.meetingType = data.item.meetingType
		//走手动呼叫类型
		if (callType === 'audio') {
			user.bitrate = CONSTANT.bitrate.addByAudio
		}
	}
	if (isMcc && isSuperMeeting) {
		//会控超大方会议默认添加互动终端
		user.interactiveMt = 1
	}
	return user
}
export function tempSelectedMtEntityTranslator(
	searchVal: string,
	{ isMcc, isSuperMeeting, maxRate }: Pick<InitOption, 'isMcc' | 'isSuperMeeting' | 'maxRate'>
) {
	let user: SelectMtEntity = {
		account: searchVal,
		name: searchVal,
		bitrate: maxRate,
		bitrateType: 0,
		protocol: CONSTANT.callType.AUTO,
	}
	if (CONSTANT.reg.regIP.test(searchVal)) {
		user.accountType = CONSTANT.accountType.IP
	} else if (CONSTANT.reg.e164Ip.test(searchVal)) {
		user.accountType = CONSTANT.accountType.e164Ip
	} else if (CONSTANT.reg.sipUrl2.test(searchVal)) {
		user.accountType = CONSTANT.accountType.email
	} else if (CONSTANT.reg.mobilePhoneReg.test(searchVal)) {
		user.accountType = CONSTANT.accountType.phone
		user.mobilePhone = searchVal
	} else {
		user.accountType = CONSTANT.accountType.sat
	}
	if (isMcc && isSuperMeeting) {
		//会控超大方会议默认添加互动终端
		user.interactiveMt = 1
	}
	return user
}
export function makeSelectedMtModelListTranslator(
	{
		selectedUsers,
		selectedTemplates,
	}: { selectedUsers: PendingMtModel<DepartmentUser>[]; selectedTemplates: PendingMtModel<Template>[] },
	{ isMcc, isSuperMeeting, maxRate }: Pick<InitOption, 'isMcc' | 'isSuperMeeting' | 'maxRate'>
) {
	//去重
	let _selectedUsers = uniqBy(selectedUsers, (x) => x.item.moid)
	let _selectedTemplates = uniqBy(selectedTemplates, (x) => x.item.e164)
	const userList = _selectedUsers.map((x) => {
		let user = selectedMtEntityTranslator(x, 'auto', { isMcc, isSuperMeeting, maxRate })
		return new SelectedMtModel(user)
	})
	const templateList = _selectedTemplates.map((x) => {
		let user = selectedMtEntityTranslator(x, 'auto', { isMcc, isSuperMeeting, maxRate })
		return new SelectedMtModel(user)
	})
	return [...userList, ...templateList]
}
